/*
 * Copyright 2014 Hieu Rocker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.emoji.emoji;

import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

/**
 * Emojicon
 *
 * @author Hieu Rocker (rockerhieu@gmail.com)
 */
public class Emojicon implements Sequenceable {
    private int icon;

    private char value;

    private String emoji;

    /**
     * 构造函数
     *
     * @param icon 图标
     * @param value 值
     * @param emoji emoji文字
     */
    public Emojicon(int icon, char value, String emoji) {
        this.icon = icon;
        this.value = value;
        this.emoji = emoji;
    }

    /**
     * 构造函数
     *
     * @param in Parcel
     */
    public Emojicon(Parcel in) {
        this.icon = in.readInt();
        this.value = (char) in.readInt();
        this.emoji = in.readString();
    }

    private Emojicon() {
    }

    /**
     * 构造函数
     *
     * @param emoji 表情String
     */
    public Emojicon(String emoji) {
        this.emoji = emoji;
    }

    /**
     * 表情转换
     *
     * @param codePoint 表情码
     * @return 表情对象
     */
    public static Emojicon fromCodePoint(int codePoint) {
        Emojicon emoji = new Emojicon();
        emoji.emoji = newString(codePoint);
        return emoji;
    }

    /**
     * 表情转换
     *
     * @param codePoints 表情码集合
     * @return 表情对象
     */
    public static Emojicon fromCodePoint(int[] codePoints) {
        Emojicon emoji = new Emojicon();
        emoji.emoji = new String(codePoints, 0, codePoints.length);
        return emoji;
    }

    /**
     * 表情转换
     *
     * @param ch char
     * @return 表情对象
     */
    public static Emojicon fromChar(char ch) {
        Emojicon emoji = new Emojicon();
        emoji.emoji = Character.toString(ch);
        return emoji;
    }

    /**
     * newString
     *
     * @param codePoint 表情码
     * @return 转换后String
     */
    public static String newString(int codePoint) {
        if (Character.charCount(codePoint) == 1) {
            return String.valueOf(codePoint);
        } else {
            return new String(Character.toChars(codePoint));
        }
    }

    public String getEmoji() {
        return emoji;
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof Emojicon && emoji.equals(((Emojicon) o).emoji);
    }

    @Override
    public int hashCode() {
        return emoji.hashCode();
    }

    @Override
    public boolean marshalling(Parcel parcel) {
        parcel.writeInt(icon);
        parcel.writeInt(value);
        parcel.writeString(emoji);
        return true;
    }

    @Override
    public boolean unmarshalling(Parcel parcel) {
        return false;
    }
}
